package com.open.capacity.salary.controller;

import java.math.BigDecimal;
import java.util.Map;

import com.open.capacity.commons.Result;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.salary.entity.GzTax;
import com.open.capacity.salary.service.GzTaxService;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * 工资系统税率设置表
 *
 * @author
 * @email
 * @date 2021-08-02 14:26:51
 */
@RestController
@RequestMapping("gztaxs")
@Api(tags = "工资系统税率设置表")
public class GzTaxController {

    @Autowired
    private GzTaxService gzTaxService;

    /**
     * 初始化默认数据
     */
    @RequestMapping("/init")
    @PreAuthorize("hasAnyAuthority('gz_tax:gztax:init')")
    public Result init(String companyId) {
        gzTaxService.init(companyId);
        return Result.succeed("初始化成功");
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('gz_tax:gztax:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = gzTaxService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('gz_tax:gztax:list')")
    public Result findById(String id) {
        return Result.succeed(gzTaxService.findById(id));
    }

    /**
     * 计算税额
     */
    @RequestMapping("/calcTax")
    @PreAuthorize("hasAnyAuthority('gz_tax:gztax:list')")
    public Result calcTax(String companyId, Integer isIncludeTax, BigDecimal income) {
        return Result.succeed(gzTaxService.calcTax(companyId, isIncludeTax, income));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('gz_tax:gztax:save')")
    public Result save(@RequestBody @Valid GzTax gzTax, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            gzTaxService.save(gzTax);
        } else {
            gzTax.setId(keyValue);
            gzTaxService.update(gzTax);
        }
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('gz_tax:gztax:delete')")
    public Result delete(@PathVariable String id) {
        gzTaxService.delete(id);
        return Result.succeed("删除成功");
    }

}
